package LeetCode;

import java.util.LinkedList;
import java.util.Queue;

public class No98 {
    private TreeNode max;
    public static void main(String[] args) {
        TreeNode root = new TreeNode(5);
        root.left = new TreeNode(1);
        root.left.right = new TreeNode(3);
        root.left.right.right = new TreeNode(4);
        root.right = new TreeNode(6);
    }
    public boolean isValidBST(TreeNode root) {
//        if(root==null) return true;
//        boolean left = isValidBST(root.left);
//        if(!left) return left;
//        if(max!=null&&max.val>=root.val) return false;
//        max = root;
//        boolean right = isValidBST(root.right);
//        return right;
        if(root==null) return true;
        Queue<TreeNode> queue = new LinkedList<>();
        visitNode(root,queue);
        int min =queue.poll().val;
        while(queue.size()>0){
            int n = queue.poll().val;
            if(min>=n) return false;
            min=n;
        }
        return true;
    }

    public void visitNode(TreeNode node, Queue<TreeNode> queue){
        if(node==null) return;
        if(node.left!=null) visitNode(node.left,queue);
        queue.add(node);
        if(node.right!=null) visitNode(node.right,queue);
    }
}
